In this center next videos you will learn how to create sliding windows are moving windows to calculate
rolling statistics like simple moving average as slow and finance.
It is tradition to work with rolling statistics that are often also called financial indicators or financial
studies and rolling statistics are actually heavily used in particular and technical chart analysis
but also for performance reporting.
So what do I mean with moving windows and rolling statistics.
Let's assume that we have daily prices for the S&P 500 index for the last 10 years and we want to know
on each and every day.
The mean or the average stock price of the directly preceding 50 days so on each and every day our window
with 50 daily timestamps moves forward by one day covering their respective day and the forty nine directly
preceding days and from the set.
That we can calculate the mean stock price or the median or the max or the min so the statistic is actually
flexible here.
And by doing so we can actually smooth our data.
But we are also able to capture some recent trends in our price data.
So let's start with an example.
This is always the easiest way and we import pandas and met plop lip and we use the Seabourn style and
then we are importing S&P 500 price data from the CSC profile S&P 500 dot see a sphere.
And as always we try to convert the daytime information to a daytime data type with past dates and we
want to have a date time index.
And also here we pass day to the next column so let's do this.
And by doing so we're actually saving our data frame in the variable S&P 500 and 7.
So these are the first five timestamps starting in the year nineteen hundred seventy and onto the year
2000 and 18.
So the end of the year.
And let's also have a look here at the info method.
So we have twelve thousand one hundred and seven timestamps here and a day time index.
Now let's assume that we only want to work with the closing prices.
So we select the column close.
So this would be a pen series and we can transform a panda series where data frame with the method to
frame and by doing so we're actually overwriting here our variable.
Let's do this.
And let's again have a look here.
So these are the first five timestamps with the first five closing prices of the S&P 500 index.
Now we can also create a chart or a plot with the historic prices by using here the plot method and
the use of fixed size of twelve eight and a font size of 15.
And we also want FBA legend.
So let's have a look here.
So this is here.
Our price chart.
And apparently it wasn't too bad idea to invest in the S&P 500 index in the 70s.
So by doing so we would have increased our investment from let's say 100 to about 2000.
300 and this is still the performance without dividends.
So we do not use yet the adjusted clause but only the clothes and let us decide that we only want to
focus on the last 10 years and select only the timestamps from the thirty first of December 2008 and
to the last day of the year 2000 and 18 and be over right.
Actually our variable here and let's assume that for each and every day of the last 10 years you want
to have a rolling window of 10 times stands for 10 days.
So on each and every day you want to look back on the last nine days.
And actually the respective day.
So this is actually then a moving windows with the main window 10 and in Penang we can actually do this
with the rolling method.
So we have our data from S&P 500 and then we will see the rolling method and we can pass the a 10 to
the windows power meter so let's uh have a look here.
And by doing so we're actually creating a new object of the type or the class Panda's car windows rolling.
So in the background for each and every time stamp panels actually aggregates the directly preceding
a 10 timestamp so we have a rolling window of 10.
And that's again have a look here at the first 15 rows of our S&P 500 data frame.
So this might be quite helpful in a few minutes so we are starting here with the very last day in 2008
and then we have the second of January 2009.
Now let's assume that we want to create here a rolling window of 10 and we are doing the say actually
by applying the rolling method and then we have to change another method we have we have to define how
we want to aggregate actually our window.
So we have actually on each time stamp a set of 10 timestamps and we actually have to define which statistic
we want to calculate.
And for example we can change the mean method and then we are actually calculating the simple moving
average.
So let's do this here and we can see here that um our new data frame starts here with the any end values.
And then here on the 14th of January we have actually the very first the simple moving average or the
very first rolling statistic so apparently on the first nine times stamps welfare and a general use.
And this is simply the case so let's have a look at the very first time stamp.
So here we do not have um the 10 preceding time stamps.
So we only have here one time stamp then for the second time stamp.
We only have two time stamps.
Father rolling statistics then for the third time stamp.
We only have three preceding timestamps and data.
So we cannot calculate a rolling statistic with a window of 10.
And then finally we have the 14th of January it's the tenth time stamp and here we have in total 10
timestamps that are immediately preceding.
And here we are able actually to calculate uh the mean stock price for these 10 times stamps and instead
of calculating the mean we can also calculate the median.
So this is no problem.
And of course we could also calculate uh the maximum.
So the highest stock price of the preceding ten days that's the max method also the minimum with the
main method.
So now let's go back to the million
and actually we have seen here for the first nine time stamps that we get any n values because uh there's
another parameter for the rolling method that is the min periods parameter and by default the argument
for the min periods parameter is actually the same mass.
So the argument for the windows power meter.
So if you pass tend to the windows parameter then by default the min periods parameter is also 10 so
let's have a look at the documentation
so we have here the windows parameter and then the min periods parameter.
And it sets the minimum number of observations and window required to have a value.
Otherwise the result is an A.
So alternatively we could also say that we need a minimum number of observations of a five to calculate
our rolling statistics so let's have a look here and then we can see here that only for the first the
fourth time stamps we have here and well use and then for the fifth time stamp we have in total five
immediately preceding timestamps.
And therefore as we set here minimum periods to five then we can calculate here a rolling statistic.
So this was the introduction on moving windows rolling statistics and simple moving averages and then
the next videos we are going deeper into this topic.
So hope to see you there by.